package systemController

import (
	"CommonsProject/models/systemModel"
	httpTool "CommonsProject/pkg/tools/httpTool"
	systemService "CommonsProject/service/systemService"
	"errors"
	"github.com/gin-gonic/gin"
	"log"
	http "net/http"
	"strconv"
)

type SysUserController struct {
	httpTool.BaseController
	Service *systemService.SysUserService
}

// Register 路由注册
func (controller *SysUserController) Register(ctx *gin.Engine) {
	g := ctx.Group("/user")

	{
		g.POST("", controller.AddSysUser)
		g.DELETE(":id", controller.RemoveSysUser)
		g.PUT("", controller.ModifySysUser)
		g.GET("", controller.SelectSysUser)
		g.GET(":id", controller.SelectSysUserById)
	}

}

// AddSysUser 添加用户
func (controller *SysUserController) AddSysUser(ctx *gin.Context) {
	var user systemModel.SysUser
	controller.ShouldBandingJson(ctx, &user)
	controller.Service.AddSysUser(&user)
	ctx.JSON(http.StatusOK, controller.SuccessMsg("添加用户成功！"))
}

// RemoveSysUser 删除用户
func (controller *SysUserController) RemoveSysUser(ctx *gin.Context) {
	id, err := strconv.Atoi(ctx.Param("id"))
	if err != nil {
		panic(errors.New("不存在该id " + ctx.Param("id")))
	}

	controller.Service.RemoveSysUser(id)
	ctx.JSON(http.StatusOK, controller.SuccessMsg("删除用户成功！"))
}

// ModifySysUser 修改用户
func (controller *SysUserController) ModifySysUser(ctx *gin.Context) {
	var user systemModel.SysUser
	controller.ShouldBandingJson(ctx, &user)
	controller.Service.ModifySysUser(&user)
	ctx.JSON(http.StatusOK, controller.SuccessMsg("更新用户成功！"))
}

// SelectSysUserById 查询用户通过ID
func (controller *SysUserController) SelectSysUserById(ctx *gin.Context) {
	id, err := strconv.Atoi(ctx.Param("id"))
	if err != nil {
		panic(errors.New("参数错误！"))
	}
	pageInfo := controller.Service.GetSysUserById(id)
	ctx.JSON(http.StatusOK, controller.SuccessData(pageInfo))
}

// SelectSysUser 查询用户
func (controller *SysUserController) SelectSysUser(ctx *gin.Context) {
	size, num := controller.GetPage(ctx)
	var user systemModel.SysUser
	err := ctx.ShouldBind(&user)
	if err != nil {
		log.Println(err.Error())
	}
	pageInfo := controller.Service.GetSysUserList(&user, num, size)
	ctx.JSON(http.StatusOK, controller.SuccessData(pageInfo))
}
